package com.yfker.td.factory;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonWriter;
import com.tinder.scarlet.Message;
import com.tinder.scarlet.MessageAdapter;
import com.yfker.td.data.RealTimeModel;
import com.yfker.td.data.Result;
import com.yfker.td.data.response.AddBottleModel;
import com.yfker.td.data.response.BeginInfusionModel;
import com.yfker.td.data.response.BindBedModel;
import com.yfker.td.data.response.BindPatientModel;
import com.yfker.td.data.response.EndInfusionModel;
import com.yfker.td.data.response.ExitPutBottleConfirmModel;
import com.yfker.td.data.response.PauseInfusionConfirmModel;
import com.yfker.td.data.response.PauseInfusionModel;
import com.yfker.td.data.response.PrePutBottleConfirmModel;
import com.yfker.td.data.response.StopInfusionConfirmModel;
import com.yfker.td.data.sync.ConfirmPutBottleWrapper;
import com.yfker.td.data.sync.ExitPutBottleWrapper;
import com.yfker.td.data.sync.HeartbeatModel;
import com.yfker.td.data.sync.PrePutBottleWrapper;
import com.yfker.td.data.sync.SyncEndInfusionWrapper;
import com.yfker.td.data.sync.SyncEquipmentInfoWrapper;
import com.yfker.td.data.sync.SyncPauseInfusionWrapper;
import com.yfker.td.data.sync.UnBindWrapper;
import com.yfker.td.data.sync.UnbindModel;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;

import okio.Buffer;

public class GsonMessageAdapter implements MessageAdapter<RealTimeModel> {
    private final Gson gson;

    public GsonMessageAdapter(Gson gson) {
        this.gson = gson;
    }

    public RealTimeModel fromMessage(@NonNull Message message) {
        String stringValue = null;
        if (message instanceof Message.Text) {
            stringValue = ((Message.Text)message).getValue();
        } else if (message instanceof Message.Bytes) {
            stringValue = new String(((Message.Bytes)message).getValue());
        }

        Result result = this.jsonValid(stringValue);
        if (result == null) {
            return null;
        } else {
            String name = result.getName().getAsString();
            JsonElement body = result.getBody();
            if ("bindBed".equals(name)) {
                return (RealTimeModel)this.gson.fromJson(body, (new TypeToken<BindBedModel>() {
                }).getType());
            } else if ("bindPatient".equals(name)) {
                return (RealTimeModel)this.gson.fromJson(body, (new TypeToken<BindPatientModel>() {
                }).getType());
            } else if ("addBottle".equals(name)) {
                return (RealTimeModel)this.gson.fromJson(body, (new TypeToken<AddBottleModel>() {
                }).getType());
            } else if ("unbind".equals(name)) {
                return (RealTimeModel)this.gson.fromJson(body, (new TypeToken<UnbindModel>() {
                }).getType());
            } else if ("begin".equals(name)) {
                return (RealTimeModel)this.gson.fromJson(body, (new TypeToken<BeginInfusionModel>() {
                }).getType());
            } else if ("pause".equals(name)) {
                return (RealTimeModel)this.gson.fromJson(body, (new TypeToken<PauseInfusionModel>() {
                }).getType());
            } else if ("endInfusion".equals(name)) {
                return (RealTimeModel)this.gson.fromJson(body, (new TypeToken<EndInfusionModel>() {
                }).getType());
            } else if ("preparedConfirmed".equals(name)) {
                return (RealTimeModel)this.gson.fromJson(body, (new TypeToken<PrePutBottleConfirmModel>() {
                }).getType());
            } else if ("exitPreparedConfirmed".equals(name)) {
                return (RealTimeModel)this.gson.fromJson(body, (new TypeToken<ExitPutBottleConfirmModel>() {
                }).getType());
            } else if ("pauseConfirmed".equals(name)) {
                return (RealTimeModel)this.gson.fromJson(body, (new TypeToken<PauseInfusionConfirmModel>() {
                }).getType());
            } else {
                return "endInfusionConfirmed".equals(name) ? (RealTimeModel)this.gson.fromJson(body, (new TypeToken<StopInfusionConfirmModel>() {
                }).getType()) : null;
            }
        }
    }

    private Result jsonValid(String json) {
        JsonElement jsonElement = (new JsonParser()).parse(json);
        boolean isJsonObject = jsonElement.isJsonObject();
        if (isJsonObject) {
            Result result = new Result();
            JsonObject jsonObject = jsonElement.getAsJsonObject();
            JsonElement tag = jsonObject.get("name");
            result.setName(tag);
            JsonElement body = jsonObject.get("body");
            result.setBody(body);
            return result;
        } else {
            return null;
        }
    }

    @NonNull
    public Message toMessage(RealTimeModel t) {
        Buffer buffer = new Buffer();
        Writer writer = new OutputStreamWriter(buffer.outputStream(), StandardCharsets.UTF_8);
        TypeToken typeToken;
        TypeAdapter gsonAdapter;
        JsonWriter jsonWriter;
        String stringValue;
        if (t instanceof SyncEquipmentInfoWrapper) {
            try {
                SyncEquipmentInfoWrapper wrapper = (SyncEquipmentInfoWrapper)t;
                typeToken = new TypeToken<SyncEquipmentInfoWrapper>() {
                };
                gsonAdapter = this.gson.getAdapter(typeToken);
                jsonWriter = this.gson.newJsonWriter(writer);
                gsonAdapter.write(jsonWriter, wrapper);
                jsonWriter.close();
            } catch (IOException var8) {
                var8.printStackTrace();
            }

            stringValue = buffer.readByteString().utf8();
            return new Message.Text(stringValue);
        } else if (t instanceof UnBindWrapper) {
            try {
                UnBindWrapper wrapper = (UnBindWrapper)t;
                typeToken = new TypeToken<UnBindWrapper>() {
                };
                gsonAdapter = this.gson.getAdapter(typeToken);
                jsonWriter = this.gson.newJsonWriter(writer);
                gsonAdapter.write(jsonWriter, wrapper);
                jsonWriter.close();
            } catch (IOException var9) {
                var9.printStackTrace();
            }

            stringValue = buffer.readByteString().utf8();
            return new Message.Text(stringValue);
        } else if (t instanceof SyncEndInfusionWrapper) {
            try {
                SyncEndInfusionWrapper wrapper = (SyncEndInfusionWrapper)t;
                typeToken = new TypeToken<SyncEndInfusionWrapper>() {
                };
                gsonAdapter = this.gson.getAdapter(typeToken);
                jsonWriter = this.gson.newJsonWriter(writer);
                gsonAdapter.write(jsonWriter, wrapper);
                jsonWriter.close();
            } catch (IOException var10) {
                var10.printStackTrace();
            }

            stringValue = buffer.readByteString().utf8();
            return new Message.Text(stringValue);
        } else if (t instanceof ConfirmPutBottleWrapper) {
            try {
                ConfirmPutBottleWrapper wrapper = (ConfirmPutBottleWrapper)t;
                typeToken = new TypeToken<ConfirmPutBottleWrapper>() {
                };
                gsonAdapter = this.gson.getAdapter(typeToken);
                jsonWriter = this.gson.newJsonWriter(writer);
                gsonAdapter.write(jsonWriter, wrapper);
                jsonWriter.close();
            } catch (IOException var11) {
                var11.printStackTrace();
            }

            stringValue = buffer.readByteString().utf8();
            return new Message.Text(stringValue);
        } else if (t instanceof HeartbeatModel) {
            HeartbeatModel heartbeatModel = (HeartbeatModel)t;
            String body = heartbeatModel.getBody();

            try {
                writer.write(body);
                writer.close();
            } catch (IOException var12) {
                var12.printStackTrace();
            }

            stringValue = buffer.readByteString().utf8();
            return new Message.Text(stringValue);
        } else if (t instanceof PrePutBottleWrapper) {
            try {
                PrePutBottleWrapper wrapper = (PrePutBottleWrapper)t;
                typeToken = new TypeToken<PrePutBottleWrapper>() {
                };
                gsonAdapter = this.gson.getAdapter(typeToken);
                jsonWriter = this.gson.newJsonWriter(writer);
                gsonAdapter.write(jsonWriter, wrapper);
                jsonWriter.close();
            } catch (IOException var13) {
                var13.printStackTrace();
            }

            stringValue = buffer.readByteString().utf8();
            return new Message.Text(stringValue);
        } else if (t instanceof ExitPutBottleWrapper) {
            try {
                ExitPutBottleWrapper wrapper = (ExitPutBottleWrapper)t;
                typeToken = new TypeToken<ExitPutBottleWrapper>() {
                };
                gsonAdapter = this.gson.getAdapter(typeToken);
                jsonWriter = this.gson.newJsonWriter(writer);
                gsonAdapter.write(jsonWriter, wrapper);
                jsonWriter.close();
            } catch (IOException var14) {
                var14.printStackTrace();
            }

            stringValue = buffer.readByteString().utf8();
            return new Message.Text(stringValue);
        } else if (t instanceof SyncPauseInfusionWrapper) {
            try {
                SyncPauseInfusionWrapper wrapper = (SyncPauseInfusionWrapper)t;
                typeToken = new TypeToken<SyncPauseInfusionWrapper>() {
                };
                gsonAdapter = this.gson.getAdapter(typeToken);
                jsonWriter = this.gson.newJsonWriter(writer);
                gsonAdapter.write(jsonWriter, wrapper);
                jsonWriter.close();
            } catch (IOException var15) {
                var15.printStackTrace();
            }

            stringValue = buffer.readByteString().utf8();
            return new Message.Text(stringValue);
        } else {
            return null;
        }
    }

    public static final class Factory implements com.tinder.scarlet.MessageAdapter.Factory {
        private final Gson gson = new Gson();

        public Factory() {
        }

        @NonNull
        public MessageAdapter<?> create(@NonNull Type type, @NonNull Annotation[] annotations) {
            return new GsonMessageAdapter(this.gson);
        }
    }
}
